package com.whfc.wxmp.api.base.controller;

import com.whfc.base.dto.AppWeatherDTO;
import com.whfc.base.dto.AppWeatherLogDTO;
import com.whfc.base.dto.AppWeatherScheduleDTO;
import com.whfc.base.dto.WeatherDTO;
import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.base.service.WxWeatherService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 天气预报
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年10月25日
 */
@RestController
@RequestMapping("/mp/api")
public class WxWeatherController extends AbstractBaseController {

    @Autowired
    private WxWeatherService wxWeatherService;

    /**
     * 获取天气预报
     *
     * @param loginUser
     * @param deptId
     * @return
     */
    @GetMapping(value = {"weather/current", "/getWeather"})
    public Result getWeather(@CurrentWxUser WxUser loginUser,
                             @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}", loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取天气预报", RequestConetxtUtil.getParameters());
        WeatherDTO appWeatherDTO = wxWeatherService.getWeather(deptId);
        return ResultUtil.success(appWeatherDTO);
    }

    @GetMapping(value = {"weather/history", "/weather/log"})
    public Result getWeatherLog(@CurrentWxUser WxUser loginUser,
                                @RequestParam("deptId") Integer deptId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取历史天气预报", RequestConetxtUtil.getParameters());

        ListData<AppWeatherLogDTO> listData = wxWeatherService.getWeatherLog(deptId, date);
        return ResultUtil.success(listData);
    }


    /**
     * 2.0 获取天气预报以及日程
     *
     * @param deptId
     * @return
     */
    @GetMapping(value = {"weather/schedule", "/weatherSchedule"})
    public R<List<AppWeatherDTO>> getWeatherAndSchedule(
                                                        @RequestParam("deptId") Integer deptId,
                                                        @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("time") Date time) {
        logger.info("获取历史天气预报(按月,每天3个时间段),loginer:{},deptId:{},time：{}",  deptId, time);
        //获取天气预报
        List<AppWeatherScheduleDTO> data = wxWeatherService.getMonthWeatherSchedule(deptId, time,getUserId());
        return R.success(data);
    }



}
